package cn.yunbamboos.mybatis.metadata;

import cn.yunbamboos.mybatis.annotation.FieldStrategy;
import cn.yunbamboos.mybatis.annotation.TableField;
import org.apache.ibatis.mapping.ParameterMapping;
import org.apache.ibatis.mapping.ResultMapping;
import org.apache.ibatis.session.Configuration;

import java.lang.reflect.Field;

public class TableFieldInfo extends BaseInfo {

    private final FieldStrategy updateStrategy;

    public TableFieldInfo(Field field, TableField column){
        super(field, column.value(), field.getName(), field.getType(), column.whereStrategy(), column.sort());
        this.updateStrategy = column.updateStrategy();
    }

    /**
     * 获取 ResultMapping
     *
     * @param configuration Configuration
     * @return ResultMapping
     */
    ResultMapping getResultMapping(final Configuration configuration) {
        ResultMapping.Builder builder = new ResultMapping.Builder(configuration, this.property, this.column, this.propertyType);
        return builder.build();
    }

    ParameterMapping getParameterMapping(final Configuration configuration, final String resultMapId) {
        ParameterMapping.Builder builder = new ParameterMapping.Builder(configuration, this.property, this.propertyType);
        builder.resultMapId(resultMapId);
        return builder.build();
    }

    public FieldStrategy getUpdateStrategy() {
        return updateStrategy;
    }
}
